import pygame
from support import import_folder
from settings import *

class Effect(pygame.sprite.Sprite):
    def __init__(self, pos, animation_frames, groups):
        super().__init__(groups)
        self.frame_index = 0

        self.frames = animation_frames
        self.image = self.frames[self.frame_index]
        self.rect = self.image.get_rect(center=pos)
        self.z = LAYERS['main']
        
    def animate(self, dt):
        self.frame_index += 8*dt
        if self.frame_index >= len(self.frames):
            self.kill()  # 动画播放完毕后销毁特效
        else:
            self.image = self.frames[int(self.frame_index)]
        
    def update(self, dt):
        self.animate(dt)

class DashEffect(Effect):
    def __init__(self, pos, groups):
        # 加载分身动画
        self.frames = import_folder('./graphics/character/dash')
        super().__init__(pos, self.frames, groups)
        #self.animation_speed = 0.3  # 可以调整动画速度

class LevelUpEffect(Effect):
    def __init__(self, pos, groups, player):
        # 加载升级动画
        self.frames = import_folder('./graphics/character/level_up')
        super().__init__(pos, self.frames, groups)

        self.player = player  # 保存对玩家的引用
        
    def update(self, dt):
        # 更新位置到玩家位置
        self.rect.center = self.player.rect.center
        # 更新动画
        self.animate(dt)